﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Xml.Linq;
using Microsoft.Office.Interop;
using System.Collections.ObjectModel;
using Microsoft.Office.Interop.OneNote;


namespace ShowNotesGetter
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : System.Windows.Window
    {
        public ObservableCollection<OnePage> pages = new ObservableCollection<OnePage>();

        public Microsoft.Office.Interop.OneNote.Application2 onenoteApp;
        public class OnePage
        {
            public string PageName { get; set; }
            //public OneNoteObjectId PageID { get; set; }
            public string PageId { get; set;  }

        }

        public MainWindow()
        {
            InitializeComponent();
            onenoteApp = new Microsoft.Office.Interop.OneNote.Application2Class();
        }

        //private void button1_Click(object sender, RoutedEventArgs e)
        //{

        //    foreach (var notebook in OneNoteHierarchy.Current.Notebooks)
        //    {
        //        textBox1.Text += "Notebook:" + notebook.Name + Environment.NewLine;

        //        if (notebook.Name.ToUpperInvariant() == notebookNameTextBox.Text.ToUpperInvariant())
        //        { 
        //            foreach (var section in notebook.Sections)
        //            {
        //                textBox1.Text += "  Section:" + section.Name + Environment.NewLine;

        //                foreach (var s in section.Sections)
        //                {
        //                    textBox1.Text += "    Section:" + s.Name + Environment.NewLine;
        //                    foreach (var p in s.Pages)
        //                    {
        //                        textBox1.Text += "      Page:" + p.Name + Environment.NewLine;
        //                        string pagename = notebook.Name + "-" + section.Name + "-" + s.Name + "-" + p.Name;
        //                        pages.Add(new OnePage() {PageName = pagename,PageID = p.ObjectId });
        //                    }

        //                }
                    
        //                foreach (var p in section.Pages )
        //                {
        //                    textBox1.Text += "  Page:" + p.Name + Environment.NewLine;
        //                    string pagename = notebook.Name + "-" + section.Name + "-" + p.Name;
        //                    pages.Add(new OnePage() {PageName = pagename,PageID = p.ObjectId });
           
        //                }
        //            }
        //        }
        //    }

        //    comboBox1.ItemsSource = null;
        //    comboBox1.ItemsSource = pages;
        //}

        //private void comboBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
        //{
        //    var pageX = OneNoteApplication.Current.GetPageContent((OneNoteObjectId)comboBox1.SelectedValue);
        //    string copyHtml = "<ul>";
        //    textBox1.Text = "";

        //    var search = lineSearchTextTextBox.Text;

        //    var ns = pageX.Root.Name.Namespace;
        //    var shownotesitems = pageX.Descendants(ns + "T").Where(n =>
        //      n.Value.Contains(search));

        //    if (shownotesitems.Count() == 0)
        //        return;

        //    foreach (var item in shownotesitems)
        //    {
        //        //Clean up
        //        string working = item.Value.Replace(search, "");
        //        working = working.Replace("<span\nstyle='background:#FFFF99'>", "");
        //        working = working.Replace("</span>", "");
        //        working = working.Replace("\n", " ");
        //        textBox1.Text += working + Environment.NewLine;
        //        copyHtml += "<li>" + working;
        //        copyHtml += "</li>";
        //    }
        //    copyHtml += "</ul>";
        //    ClipboardHelper.CopyHtmlToClipBoard(copyHtml);
        //}

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            string notebookXml;
            onenoteApp.GetHierarchy(null, HierarchyScope.hsNotebooks, out notebookXml);
            
            var doc = XDocument.Parse(notebookXml);
            var ns = doc.Root.Name.Namespace;

            foreach (var notebookNode in from node in doc.Descendants(ns + "Notebook") select node)
            {
                var notebookname = notebookNode.Attribute("name").Value;
                textBox1.Text += "Notebook:" + notebookNode.Attribute("name").Value + Environment.NewLine;

                if (notebookname.ToUpperInvariant() == notebookNameTextBox.Text.ToUpperInvariant())
                {
                    string noteboodId;
                    noteboodId = notebookNode.Attribute("ID").Value;

                    string sectionXml;
                    onenoteApp.GetHierarchy(noteboodId, HierarchyScope.hsSections, out sectionXml);
                    var section = XDocument.Parse(sectionXml);

                    foreach (var sectionNode in from sec in section.Descendants(ns + "Section") select sec)
                    {
                        string sectionName = sectionNode.Attribute("name").Value;
                        textBox1.Text += "    Section:" + sectionNode.Attribute("name").Value + Environment.NewLine; 

                        string sectionId;
                        sectionId = sectionNode.Attribute("ID").Value;
                        
                        string pageXml;
                        onenoteApp.GetHierarchy(sectionId, HierarchyScope.hsPages, out pageXml);
                        var page = XDocument.Parse(pageXml);

                        foreach (var pageNode in from pg in page.Descendants(ns + "Page") select pg)
                        {
                            string pageName = pageNode.Attribute("name").Value;

                            textBox1.Text += "      Page:" + pageNode.Attribute("name").Value + Environment.NewLine;
                            string pagename = notebookname + "-" + sectionName + "-" + pageName;
                            pages.Add(new OnePage() { PageName = pagename, PageId = pageNode.Attribute("ID").Value });
                        }

                    }

                }
            }
 
            //foreach (var notebook in OneNoteHierarchy.Current.Notebooks)
            //{
            //    textBox1.Text += "Notebook:" + notebook.Name + Environment.NewLine;

            //    if (notebook.Name.ToUpperInvariant() == notebookNameTextBox.Text.ToUpperInvariant())
            //    {
            //        foreach (var section in notebook.Sections)
            //        {
            //            textBox1.Text += "  Section:" + section.Name + Environment.NewLine;

            //            foreach (var s in section.Sections)
            //            {
            //                textBox1.Text += "    Section:" + s.Name + Environment.NewLine;
            //                foreach (var p in s.Pages)
            //                {
            //                    textBox1.Text += "      Page:" + p.Name + Environment.NewLine;
            //                    string pagename = notebook.Name + "-" + section.Name + "-" + s.Name + "-" + p.Name;
            //                    pages.Add(new OnePage() { PageName = pagename, PageID = p.ObjectId });
            //                }

            //            }

            //            foreach (var p in section.Pages)
            //            {
            //                textBox1.Text += "  Page:" + p.Name + Environment.NewLine;
            //                string pagename = notebook.Name + "-" + section.Name + "-" + p.Name;
            //                pages.Add(new OnePage() { PageName = pagename, PageID = p.ObjectId });

            //            }
            //        }
            //    }
            //}

            comboBox1.ItemsSource = null;
            comboBox1.ItemsSource = pages;
        }

        private void comboBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var pageItem = (OnePage)comboBox1.SelectedItem;
            string pageId = pageItem.PageId;
            string pageXml;

            
            onenoteApp.GetPageContent(pageId, out pageXml);


            var pageX = XDocument.Parse(pageXml);
            string copyHtml = "<ul>";
            textBox1.Text = "";

            var search = lineSearchTextTextBox.Text;

            var ns = pageX.Root.Name.Namespace;
            var shownotesitems = pageX.Descendants(ns + "T").Where(n =>
              n.Value.Contains(search));

            if (shownotesitems.Count() == 0)
                return;

            foreach (var item in shownotesitems)
            {
                //Clean up
                string working = item.Value.Replace(search, "");
                working = working.Replace("<span\nstyle='background:#FFFF99'>", "");
                working = working.Replace("</span>", "");
                working = working.Replace("\n", " ");
                textBox1.Text += working + Environment.NewLine;
                copyHtml += "<li>" + working;
                copyHtml += "</li>";
            }
            copyHtml += "</ul>";
            ClipboardHelper.CopyHtmlToClipBoard(copyHtml);
        }


        //private void comboBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
        //{
        //    //var pageX = OneNoteApplication.Current.GetPageContent((OneNoteObjectId)comboBox1.SelectedValue);
        //    //string copyHtml = "<ul>";
        //    //textBox1.Text = "";

        //    //var search = lineSearchTextTextBox.Text;

        //    //var ns = pageX.Root.Name.Namespace;
        //    //var shownotesitems = pageX.Descendants(ns + "T").Where(n =>
        //    //  n.Value.Contains(search));

        //    //if (shownotesitems.Count() == 0)
        //    //    return;

        //    //foreach (var item in shownotesitems)
        //    //{
        //    //    //Clean up
        //    //    string working = item.Value.Replace(search, "");
        //    //    working = working.Replace("<span\nstyle='background:#FFFF99'>", "");
        //    //    working = working.Replace("</span>", "");
        //    //    working = working.Replace("\n", " ");
        //    //    textBox1.Text += working + Environment.NewLine;
        //    //    copyHtml += "<li>" + working;
        //    //    copyHtml += "</li>";
        //    //}
        //    //copyHtml += "</ul>";
        //    //ClipboardHelper.CopyHtmlToClipBoard(copyHtml);
        //}

  
    }
}
